package com.cdtye.itps.cms.dao.datasysn;

import com.cdtye.common.kafkacore.model.dto.KfkBaseDto;
import com.cdtye.common.webcore.constants.WebConstants;
import com.cdtye.common.webcore.enums.YesNoEnum;
import com.cdtye.common.webcore.util.RoadMarkUtil;
import com.cdtye.itps.cms.dao.defect.entity.JcwProblemDefectInfo;
import com.cdtye.itps.cms.dao.detect.JcjcDefectInfoSection;
import com.cdtye.itps.cms.dao.enums.DefectInfoStatusEnum;
import com.cdtye.itps.cms.dao.enums.StatusEnum;
import com.cdtye.itps.cms.dao.enums.DelFlagEnum;
import com.cdtye.itps.models.base.model.attr.basic.DirectionAttr;
import com.cdtye.itps.models.base.model.attr.basic.LineAttr;
import com.cdtye.itps.models.base.model.attr.basic.PillarAttr;
import com.cdtye.itps.models.base.model.attr.basic.SiteAttr;
import com.cdtye.itps.models.base.model.attr.basic.DevClassAttr;
import com.cdtye.itps.models.base.model.attr.defect.DefectGradeAttr;
import com.cdtye.itps.models.base.model.attr.defect.DefectPhotoAttr;
import com.cdtye.itps.models.base.model.attr.defect.UserDefectTypeAttr;
import com.cdtye.itps.models.base.model.attr.sysm.DeptAttr;
import com.cdtye.itps.models.base.model.attr.sysm.DicAttr;
import com.cdtye.itps.models.base.model.entity.defect.JcwDefectInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Author: wang lei
 * @Date: 2021/10/10 11:22
 * @Description:
 */
@ApiModel(value = "缺陷")
@Data
@Accessors(chain = true)
public class Defect extends KfkBaseDto {

    @ApiModelProperty(value = "厂商缺陷Id")
    private String extDefectId;
    @ApiModelProperty(value = "文件包Id")
    private String packageId;
    //"缺陷状态，枚举，详见表 3.18缺陷状态编码表：0：新发现、1：整改中、2：已销号、3：作废"
    @ApiModelProperty(value = "缺陷状态")
    private Integer status;
    //"装置类型，枚举, 详见表 3.19缺陷装置类型编码表1：1C、2：2C、3：3C、4：4C、5：5C、6：6C、7：其它"
    @ApiModelProperty(value = "装置类型")
    private String ctype;
    //"缺陷等级，枚举, 详见表 3.20缺陷等级编码表1：一级缺陷、2：二级缺陷"
    @ApiModelProperty(value = "缺陷等级")
    private String faultLevelCode;
    @ApiModelProperty(value = "缺陷类型编码")
    private String typeCode;
    @ApiModelProperty(value = "缺陷类型名称")
    private String typeName;
    //"缺陷分类，枚举，详见表 3.21缺陷分类编码表1：A类、2：B类、3：C类"
    @ApiModelProperty(value = "缺陷分类")
    private String defectClassCode;
    @ApiModelProperty(value = "线路编码")
    private String lineCode;
    @ApiModelProperty(value = "线路名称")
    private String lineName;
    @ApiModelProperty(value = "行别编码")
//"行别编码，枚举，详见表 3.25行别编码表 1：上行、2：下行、3：单线"
    private String lineDirectCode;
    @ApiModelProperty(value = "站区间编码")
    private String stCode;
    @ApiModelProperty(value = "站区间名称")
    private String stName;
    @ApiModelProperty(value = "支柱编码")
    private String devCode;
    @ApiModelProperty(value = "支柱号")
    private String devSetloc;
    @ApiModelProperty(value = "支柱公里标")
    private String km;
    @ApiModelProperty(value = "缺陷描述")
    private String defectDesc;
    @ApiModelProperty(value = "路局编码")
    private String railwayCode;
    @ApiModelProperty(value = "路局名称")
    private String railwayName;
    //发现时间 日期格式：yyyy-MM-dd HH:mm:ss
    @ApiModelProperty(value = "发现时间")
    private Date discoveryDate;
    //复核日期 日期格式：yyyy-MM-dd HH:mm:ss
    @ApiModelProperty(value = "复核日期")
    private Date measureDate;
    @ApiModelProperty(value = "填写人名称")
    private String registerName;
    @ApiModelProperty(value = "整改人姓名")
    private String dealPersonName;
    @ApiModelProperty(value = "复核人名称")
    private String measurePersonName;
    //"缺陷值参数信息，样例：[{        ""LCZ"": 10,        ""WD"": 12      }]"
    @ApiModelProperty(value = "缺陷值")
    private String defectValue;
    @ApiModelProperty(value = "整改方案")
    private String dealAction;
    @ApiModelProperty(value = "复核说明")
    private String measureDesc;

    @ApiModelProperty(value = "缺陷照片")
    private List<DefectPhotoAttr> picList;

    //"线路类型，枚举， 1：高速铁路、2：普速铁路"
    @ApiModelProperty(value = "线路类型")
    private String lineTypeCode;
    @ApiModelProperty(value = "设备名称")
    private String devName;
    @ApiModelProperty(value = "缺陷公里标")
    private String ftKm;
    @ApiModelProperty(value = "缺陷地点")
    private String defectLoc;
    //"是否需要现场整改，枚举, : 0：不需要、1：需要"
    @ApiModelProperty(value = "是否需要现场整改")
    private String isNeedDeal;
    @ApiModelProperty(value = "供电段编码")
    private String powerSupplySectionCode;
    @ApiModelProperty(value = "供电段名称")
    private String powerSupplySectionName;
    @ApiModelProperty(value = "所属车间编码")
    private String workShopCode;
    @ApiModelProperty(value = "所属车间名称")
    private String workShopName;
    @ApiModelProperty(value = "所属工区编码")
    private String workAreaCode;
    @ApiModelProperty(value = "所属工区名称")
    private String workAreaName;
    //填写时间 日期格式：yyyy-MM-dd HH:mm:ss
    @ApiModelProperty(value = "填写时间")
    private Date registerDate;
    //下发日期 日期格式：yyyy-MM-dd HH:mm:ss
    @ApiModelProperty(value = "下发日期")
    private Date publishDate;
    //整改时间 日期格式：yyyy-MM-dd
    @ApiModelProperty(value = "整改时间")
    private Date dealDate;
    //销号日期 日期格式：yyyy-MM-dd HH:mm:ss
    @ApiModelProperty(value = "销号日期")
    private Date endDate;
    //下发人名称
    @ApiModelProperty(value = "下发人名称")
    private String publishUserName;
    //销号人名称
    @ApiModelProperty(value = "销号人名称")
    private String endName;
    //零部件编码
    @ApiModelProperty(value = "零部件编码")
    private String partCode;
    //零部件名称
    @ApiModelProperty(value = "零部件名称")
    private String partName;
    //"缺陷图片，：name 文件名 String xx.jpg; photoUrl 照片地址 String HTTPS://xx/xx.jpg"
    @ApiModelProperty(value = "缺陷图片")
    private String defectPhoto;
    //"审批信息， 样例：[ {""name"": ""姓名"",""auditDate"": ""审批时间，yyyy-MM-dd HH:mm:ss"",auditDesc"": ""审批意见"",""auditResult"": ""审批结论,0：不通过，1：通过"",}]"
    @ApiModelProperty(value = "审批信息")
    private String auditInfo;
    //整改期限,样例：”2019-06-15”
    @ApiModelProperty(value = "整改期限")
    private Date dealDueDate;
    //"复测图片， name 文件名 String xx.jpg;photoUrl 照片地址 String HTTPS://xx/xx.jpg; photoType 照片类型 String 1：杆号照片 2：复测照片"
    @ApiModelProperty(value = "复测图片")
    private String retestPhoto;
    //"整改图片，name 文件名 String xx.jpgphotoUrl 照片地址 String HTTPS://xx/xx.jpgphotoType 照片类型 String 1：杆号照片 2：整改前照片 3：整改后照片"
    @ApiModelProperty(value = "整改图片")
    private String dealPhoto;
    //"复核值，样例： [{""setloc"": ""支柱号"",""km"": ""公里标，k100+100"",""LCZ"": 10,""WD"": 12}]"
    @ApiModelProperty(value = "复核值")
    private String measureValue;
    //"整改值，样例：  [{""setloc"": ""支柱号"",""km"": ""公里标，k100+100"", ""LCZ"": 10,""WD"": 12}]"
    @ApiModelProperty(value = "整改值")
    private String dealValue;
    //"是否典型缺陷，枚举， 0：否、1：是
    @ApiModelProperty(value = "是否典型缺陷")
    private String isFavorite;
    //是否删除
    @ApiModelProperty(value = "是否删除")
    private String isDeleted;
    @ApiModelProperty(value = "创建时间")
    private Date  createTime;
    @ApiModelProperty(value = "修改时间")
    private Date  updateTime;

    public JcwDefectInfo covEntity() {

        JcwDefectInfo model = new JcwDefectInfo();
        model.setDeptAttr(new DeptAttr().setDeptCode(this.getWorkAreaCode()).setDeptName(this.getWorkAreaName()));
        model.setLineAttr(new LineAttr().setLineCode(this.lineCode).setLineName(this.lineName));
        model.setSiteAttr(new SiteAttr().setLineSiteCode(this.stCode).setSiteName(this.stName));
        model.setDirectionAttr(new DirectionAttr().setDirectionCode(this.lineDirectCode).setDirectionName(this.lineDirectCode).setDirectionId(this.lineDirectCode));


        model.setPillarAttr(new PillarAttr().setPillarCode(this.devCode).setPillarNum(this.devSetloc));
        if(StringUtils.isNotBlank(this.faultLevelCode)){
            String userGradeName ="";
            if(StringUtils.equals(this.faultLevelCode, "1")){
                userGradeName = "一级";
            }
            if(StringUtils.equals(this.faultLevelCode,"2")){
                userGradeName = "二级";
                if(StringUtils.equals(this.defectClassCode,"1")){
                    userGradeName += "A";
                }else if(StringUtils.equals(this.defectClassCode,"2")){
                    userGradeName += "B";
                }
            }
            model.setGradeAttr(new DefectGradeAttr().setUserGradeName(userGradeName).setGradeName(userGradeName).setUserGradeId(this.faultLevelCode));
        }
        if(StringUtils.isNotBlank(this.defectDesc)) {
            model.setUserDefectTypeAttr(new UserDefectTypeAttr().setUserDefectTypeCode(this.defectDesc).setUserDefectTypeName(this.defectDesc));
        }
        model.setDefectClass(new DicAttr().setDicValue(this.typeCode).setDicKey(this.typeName));
        model.setId(this.extDefectId);
        model.setDefectValue(this.defectValue);
        model.setPicList(this.picList);
        model.setDesc(this.defectDesc);
        if (this.dealDate != null) {
            model.setRectifDt(this.dealDate);
        }
        if (StringUtils.isNotBlank(this.km)) {
            if (this.km.contains(WebConstants.CHAR_K)) {
                model.setRoadMark(RoadMarkUtil.covRoadMark(this.km));
            } else {
                model.setRoadMark(Double.valueOf(this.km));
            }
        }
        model.setDevClassAttr(new DevClassAttr().setDevClassId(this.ctype));
        model.setSourceKeyId(this.extDefectId);
        model.setFindDt(this.discoveryDate);
        model.setSolveDt(this.dealDate);
        model.setSolvePerson(this.dealPersonName);
        model.setSolveMethod(this.dealAction);
        model.setDefectStatus(this.status);
        model.setChecker(this.measurePersonName);
        model.setCheckDt(this.measureDate);
        model.setCheckDesc(this.measureDesc);
        model.setStatus(1);
        model.setDelFlag(YesNoEnum.NO.getCode());
        model.setDiscoverSourceId(this.extDefectId);
        return model;
    }

    /**
     * 转换成 局级对象
     * @param jcwProblemDefectInfo
     * @return
     */
    public JcwProblemDefectInfo getJcwProblemDefectInfo(JcwProblemDefectInfo jcwProblemDefectInfo){

        jcwProblemDefectInfo.setFindDate(this.discoveryDate);
        jcwProblemDefectInfo.setDefectValue(this.defectValue);
        jcwProblemDefectInfo.setReviewDesc(this.measureDesc);
        jcwProblemDefectInfo.setEquipType(this.devName);
        jcwProblemDefectInfo.setDefectDescription(this.defectDesc);
        jcwProblemDefectInfo.setHandleBy(this.dealPersonName);
        jcwProblemDefectInfo.setHandleDate(this.dealDate);
        jcwProblemDefectInfo.setDelFlag("N");

        //设置缺陷销号状态  0：新发现、1：整改中、2：已销号、3：作废"
        if(this.status == 2 || this.status == 3){ //已销号
            jcwProblemDefectInfo.setStatus("Y");
        }else {
            jcwProblemDefectInfo.setStatus("N");
        }
        jcwProblemDefectInfo.setDefectStatus(this.status.toString());
        jcwProblemDefectInfo.setHandleDescription(this.dealAction);
        jcwProblemDefectInfo.setCType(this.ctype);
        jcwProblemDefectInfo.setCtypeId(this.ctype);
        jcwProblemDefectInfo.setCheckDt(this.measureDate);
        jcwProblemDefectInfo.setReviewUser(this.measurePersonName);
        jcwProblemDefectInfo.setSystemSource("6C");

        jcwProblemDefectInfo.setDiscoverSourceId(this.extDefectId);
        jcwProblemDefectInfo.setCreateDatetimeSource(this.createTime);
        jcwProblemDefectInfo.setUpdateDatetimeSource(this.updateTime);

        //-----
        jcwProblemDefectInfo.setPackageId(this.packageId);
        jcwProblemDefectInfo.setLineTypeCode(this.lineTypeCode);
        jcwProblemDefectInfo.setDevName(this.devName);
        jcwProblemDefectInfo.setDefectRoadMark(this.ftKm);
        jcwProblemDefectInfo.setDefectLoc(this.defectLoc);
        if(this.isNeedDeal != null && !"".equals(this.isNeedDeal)){
            jcwProblemDefectInfo.setReviseNotice(Integer.parseInt(this.isNeedDeal));
        }
        jcwProblemDefectInfo.setRegisterDate(this.registerDate);
        jcwProblemDefectInfo.setPublishDate(this.publishDate);
        jcwProblemDefectInfo.setEndDate(this.endDate);
        jcwProblemDefectInfo.setPublishUserName(this.publishUserName);
        jcwProblemDefectInfo.setEndName(this.endName);
        jcwProblemDefectInfo.setPartCode(this.partCode);
        jcwProblemDefectInfo.setPartName(this.partName);
        jcwProblemDefectInfo.setAuditInfo(this.auditInfo);
        jcwProblemDefectInfo.setSolveDate(this.dealDueDate);
        jcwProblemDefectInfo.setRetestPhoto(this.retestPhoto);
        jcwProblemDefectInfo.setDealPhoto(this.dealPhoto);
        jcwProblemDefectInfo.setMeasureValue(this.measureValue);
        jcwProblemDefectInfo.setDealValue(this.dealValue);
        jcwProblemDefectInfo.setIsFavorite(this.isFavorite);
        return jcwProblemDefectInfo;
    }

    /**
     * 转换成 段级对象
     */
    public JcjcDefectInfoSection getJcjcDefectInfoSection(JcjcDefectInfoSection jcjcDefectInfoSection){

        if(this.status == 0 || this.status == 1){ //未销号
            jcjcDefectInfoSection.setStatus(DefectInfoStatusEnum.NO.value);
            jcjcDefectInfoSection.setDefectStatus("0");

        }
        if(this.status == 2 || this.status == 3){ //已销号
            jcjcDefectInfoSection.setStatus(DefectInfoStatusEnum.YES.value);
            jcjcDefectInfoSection.setDefectStatus("1");
        }
        jcjcDefectInfoSection.setCType(this.ctype);
        jcjcDefectInfoSection.setDiscoverSourceId(this.extDefectId);
        jcjcDefectInfoSection.setDiscoverSourceTypeId("4028812160bfa73e0160bff088530023");
        jcjcDefectInfoSection.setDiscoverDate(this.discoveryDate);
        jcjcDefectInfoSection.setDiscoverPerson(this.registerName);
        jcjcDefectInfoSection.setSolvePersonal(this.dealPersonName);
//        jcjcDefectInfoSection.setRectifDatetime(this.dealDate);
        jcjcDefectInfoSection.setDelFlag("N");
        jcjcDefectInfoSection.setCreateDatetime(this.createTime);
        jcjcDefectInfoSection.setUpdateDatetime(this.updateTime);
        jcjcDefectInfoSection.setDefectDescription(this.defectDesc);
        jcjcDefectInfoSection.setSystemSource("6C");
        jcjcDefectInfoSection.setRectifDatetime(this.dealDueDate);
        jcjcDefectInfoSection.setEndDate(this.endDate);
        jcjcDefectInfoSection.setPublishUserName(this.publishUserName);
        jcjcDefectInfoSection.setDefectValue(this.defectValue);
        jcjcDefectInfoSection.setIsFavorite(this.isFavorite);
        jcjcDefectInfoSection.setDefectLoc(this.defectLoc);
        jcjcDefectInfoSection.setPackageId(this.packageId);
        jcjcDefectInfoSection.setRegisterDate(this.registerDate);
        jcjcDefectInfoSection.setPublishDate(this.publishDate);
        jcjcDefectInfoSection.setEndName(this.endName);
        jcjcDefectInfoSection.setPartCode(this.partCode);
        jcjcDefectInfoSection.setPartName(this.partName);
        jcjcDefectInfoSection.setAuditInfo(this.auditInfo);
        jcjcDefectInfoSection.setRetestPhoto(this.retestPhoto);
        jcjcDefectInfoSection.setDealPhoto(this.dealPhoto);
        jcjcDefectInfoSection.setMeasureValue(this.measureValue);
        jcjcDefectInfoSection.setDealValue(this.dealValue);
        jcjcDefectInfoSection.setInputAuditStatus(0);
        jcjcDefectInfoSection.setRectifAuditStatus(0);
        jcjcDefectInfoSection.setCheckAuditStatus(0);
        jcjcDefectInfoSection.setDeferredStatus(0);
        return jcjcDefectInfoSection;
    }
}
